package threecreateways;

import java.util.concurrent.*;

// 创建多线程方式3，实现callable，重写call方法（有返回值）
public class Test3 implements Callable<Boolean> {

    public Boolean call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"执行开启的另一个线程");
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Test3 thread1 = new Test3();
        Test3 thread2 = new Test3();
        Test3 thread3 = new Test3();
        // 1.创建执行服务
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 2.提交执行
        Future<Boolean> r1 = executorService.submit(thread1);
        Future<Boolean> r2 = executorService.submit(thread2);
        Future<Boolean> r3 = executorService.submit(thread3);
        // 3.获取结果
        boolean res1 = r1.get();
        boolean res2 = r2.get();
        boolean res3 = r3.get();
        System.out.println(res1);
        System.out.println(res2);
        System.out.println(res3);
        // 4.关闭服务
        executorService.shutdown();


        /*
            pool-1-thread-1执行开启的另一个线程
            pool-1-thread-3执行开启的另一个线程
            pool-1-thread-2执行开启的另一个线程
            true   多了个返回值
            true
            true

         */

    }
}
